// JavaScript Document

var popupStatus = 0;
function loadPopup(codeName,operMsg){
	//判断浏览器类型
	if(window.ActiveXObject){
		//第一种解决方案：通过一个透明的iframe解决
		$("#backgroundPopup").html("<iframe style='width:100%;height:100%;filter:alpha(opacity=0);-moz-opacity:0'></iframe>");
		//第二种方案：直接隐藏所有的select
		//取得页面中所有的select对像，也就是一个数组
		//var selects = document.getElementsByTagName("select");
		//for(var i = 0; i < selects.length;i++){
		//	selects[i].style.display = "none";
		//}
	}
	var queryString = {code:codeName};
	
	operMsg = typeof operMsg == "undefined" ? "" : operMsg;
	
	$.ajax({	
		type:"post",
		url:"getDictionaryDetails.action",
		data:queryString,
		dataType:"json",
		success: function(msg) {
			$('#contactArea').html('');
			
			var tmp = new Array();
			tmp.push("<p id='contactHead'><input type='button' value='保存' onclick=updateDict('"+codeName+"') />&nbsp;&nbsp;<span style='font-size:12px;color:red;'>"+operMsg+"</span></p><br/><br/><br/>");
			tmp.push("<div id='contactBody'>");
			
			if( msg.SDictionaryDetails.length >= 1)
			{
				for(var i = 0; i < msg.SDictionaryDetails.length; i++)
					tmp.push("<br/><input type='text' class='oldDict' name='"+msg.SDictionaryDetails[i].id+"' value='"+msg.SDictionaryDetails[i].value+"'/>" +
								"<input type='button' value='向上' onclick=upsort('"+msg.SDictionaryDetails[i].sort+"','"+codeName+"') />" +
								"<input type='button' value='向下' onclick=downsort('"+msg.SDictionaryDetails[i].sort+"','"+codeName+"') />" +
								"<input type='button' value='删除' onclick=del('"+msg.SDictionaryDetails[i].id+"','"+codeName+"') />");
				$('#popupContact h1').html("编辑--"+msg.SDictionaryDetails[0].SDictionaryClass.name);
			}
			else
				$('#popupContact h1').html("编辑选项");
			
			tmp.push("<br/><input type='text' class='newDict' value=''");
			tmp.push("</div>");
			$('#contactArea').html(tmp.join(""));
			
			if( msg.SDictionaryDetails.length >= 1)
			{
				var tmpSelect = new Array();
				$('#'+codeName).html('');
				tmpSelect.push("<option value=''>---</option>");
				for( var i = 0; i < msg.SDictionaryDetails.length; i++)
					tmpSelect.push("<option value='"+msg.SDictionaryDetails[i].id+"'>"+msg.SDictionaryDetails[i].value+"</option>");
				
				$('#'+codeName).html(tmpSelect.join(""));
			}
		}
	});
	
	centerPopup();
	
	if(popupStatus == 0){
		
		$("#backgroundPopup").css({
			"opacity":"0.3"						  
		});
		$("#popupContact").fadeIn("slow");
		$("#backgroundPopup").fadeIn("slow");
		popupStatus = 1;
	}
}

function initSelect(codeName)
{
	var queryString = {code:codeName};
	
	$.ajax({	
		type:"post",
		url:"getDictionaryDetails.action",
		data:queryString,
		dataType:"json",
		success: function(msg) {
			if( msg.SDictionaryDetails.length >= 1)
			{
				var tmpSelect = new Array();
				$('#'+codeName).html('');
				tmpSelect.push("<option value=''>---</option>");
				for( var i = 0; i < msg.SDictionaryDetails.length; i++)
					tmpSelect.push("<option value='"+msg.SDictionaryDetails[i].id+"'>"+msg.SDictionaryDetails[i].value+"</option>");
				
				$('#'+codeName).html(tmpSelect.join(""));
			}
		}
	});
}

function updateDict(codeName)
{
	var oldVal = $("#contactBody .oldDict");
	
	var newVal = $("#contactBody .newDict").val();
	
	var strBuf = new Array();
	
	for( var i = 0; i < oldVal.length; i++ )
		strBuf.push(oldVal[i].name+"@#%"+oldVal[i].value+"%#@");	
		
	
	var queryString = { 
						updateData : strBuf.join(""),
						newDict	: newVal,
						code : codeName
						};
	
	alert(queryString.updateData);
	
	$.ajax({
		type:"post",
		url:"updateDictionaryDetails.action",
		data:queryString,
		dataType:"json",
		success: function(msg) {
			loadPopup(codeName,"保存成功");
		}
	});
	
}

function upsort(sort,code)
{
	var queryString = { sort : sort , code : code};
	$.ajax({
		type:"post",
		url:"upSort.action",
		data:queryString,
		dataType:"json",
		success: function(msg) {
			loadPopup(code,"向上移动成功");
		}
	});
}

function downsort(sort,code)
{
	var queryString = { sort : sort , code : code};
	$.ajax({
		type:"post",
		url:"downSort.action",
		data:queryString,
		dataType:"json",
		success: function(msg) {
			loadPopup(code,"向下移动成功");
		}
	});
}

function del(id,code)
{
	var queryString = { id : id};
	$.ajax({
		type:"post",
		url:"del.action",
		data:queryString,
		dataType:"json",
		success: function(msg) {
			loadPopup(code,"删除成功");
		}
	});
}

function centerPopup(){
	
	var windowWidth = screen.width;
	var windowHeight = document.body.scrollTop + (screen.height/2-$("#popupContact").height()/2 - 110);
	
	var popupWidth = $("#popupContact").width();
	var popupHeight = $("#popupContact").height();
	
	$("#popupContact").css({
		"position":"absolute",
		"top": windowHeight,
		"left":windowWidth/2 - popupWidth/2
	});
	
}

function disablePopup(){
	if(popupStatus == 1){
		$("#popupContact").fadeOut("slow");
		$("#backgroundPopup").fadeOut("slow");
		popupStatus = 0;
	}
}

$(document).ready(function(){
		
		$("#popupContactClose").click(function(){
			disablePopup();	
		});
		
		//$("#backgroundPopup").click(function(){
		//	disablePopup();	
		//});
	
});